<?php

/**
 * 用户需求
 */

namespace app\api\controller;


use app\common\controller\ApiController;
use app\common\lib\RespCode;
use think\Db;
use think\Exception;
use app\api\model\UserDemand as UserDemandModel;
use app\api\model\User as UserModel;

class UserDemand extends ApiController {

    protected $isArrayResult = ['showdemand'];

    protected $resubmitLock = ['editpushstatus'];

    /**
     * 添加需求和编辑需求
     * @return false|string
     */
    public function editDemand() {
        try {
            $params = $this->checkParams('area_code,city_code,p_key,a_key,type');
            if ($params === false) {
                return $this->resErr();
            }
            $re = UserDemandModel::edit($params);
            if ($re) {
                return $this->resData('操作成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 展示需求
     */
    public function showDemand() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $re = UserDemandModel::show($params);
            return $this->resData('success', $re);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除需求
     */
    public function delDemand() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) {
                return $this->resErr();
            }
            if (empty(Db::name('user_demand')->getByid($params['id'])))
                throw new Exception('数据不存在', RespCode::DATA_ERR);
            $re = UserDemandModel::del($params);
            if (!$re) {
                throw new Exception('操作失败', RespCode::QUERY_ERR);
            } else {
                return $this->resData('操作成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改推送状态
     * @return false|string
     */
    public function editPushStatus() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            UserModel::editPushStatus($params);
            return $this->resData('操作成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

}
